package com.hxht.cmp.module;

import com.github.pagehelper.Page;
import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Favorite;
import com.hxht.cmp.entity.Plan;
import com.hxht.cmp.entity.Trial;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.FavoritePojo;
import com.hxht.cmp.pojo.FavoriteResult;
import com.hxht.cmp.pojo.SearchCriteria;
import com.hxht.cmp.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gaozikun on 2017/8/15 0015.
 */
@Controller
@RequestMapping("judge")
public class JudgeModule {

    @Autowired
    private JudgeService judgeService;
    @Autowired
    private CaseReviewService caseReviewService;
    @Autowired
    private CasePlanService casePlanService;
    @Resource
    private FavoriteService favoriteService;
    @Autowired
    private CaseVideoService caseVideoService;

    @RequestMapping("index")
    public ModelAndView index() {
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");

        ModelAndView modelAndView = new ModelAndView();

        List<String> userUids = new ArrayList<>();
        userUids.add(user.getUid());
        //法官开庭数
        modelAndView.addObject("judgeNumber", judgeService.getJudgeCourtStatistical(userUids));
        //法官开庭时长
        modelAndView.addObject("judgeOpenTime", judgeService.getJudgeCourtTimeStatistical(userUids));
        //法官总容量
        modelAndView.addObject("judgeFileSize", judgeService.getJudgeFileSizeStatistical(userUids, null));
        //所在法院法官开庭时长与开庭次数排行
        modelAndView.addObject("timeRanking", judgeService.getJudgeOpeningCourtTimeRanking(user.getCourtUid()));
        modelAndView.addObject("countRanking", judgeService.getJudgeOpeningCourtNumRanking(user.getCourtUid()));
        SearchCriteria searchCriteria = new SearchCriteria();
        searchCriteria.setCourtUid(user.getCourtUid());
        searchCriteria.setJudgeList(userUids);
        searchCriteria.setStartDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        searchCriteria.setReview("0");
        modelAndView.addObject("moreCourting", judgeService.selectJudgeCasePlanOrReview(searchCriteria));
        searchCriteria.setReview("1");
        modelAndView.addObject("moreReview", judgeService.selectJudgeCasePlanOrReview(searchCriteria));

        modelAndView.setViewName("judge/index");
        return modelAndView;
    }

    @RequestMapping("more_courting")
    public ModelAndView moreCourting() {
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");

        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(user.getCourtUid());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("secretaryList", selectJudgeAndSecretary.get("secretaryList"));
        modelAndView.addObject("courtroomList", Constants.COURTROOM_LIST);
        modelAndView.addObject("caseTypeList", caseReviewService.getAllCaseType());
        modelAndView.setViewName("judge/more_courting");
        return modelAndView;
    }

    @RequestMapping("more_review")
    public ModelAndView moreReview() {
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");

        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(user.getCourtUid());
        modelAndView.addObject("secretaryList", selectJudgeAndSecretary.get("secretaryList"));
        modelAndView.addObject("courtroomList", Constants.COURTROOM_LIST);
        modelAndView.addObject("caseTypeList", caseReviewService.getAllCaseType());
        //收藏
        List<Favorite> favorites = favoriteService.selectAllFavorite(user.getUid());
        List<String> plans = new ArrayList<>();
        for (Favorite favorite:favorites) {
            plans.add(favorite.getPlanUid());
        }
        modelAndView.addObject("favorites", plans);
        modelAndView.setViewName("judge/more_review");
        return modelAndView;
    }

    @RequestMapping("living")
    public ModelAndView living(String planUid, String courtRoomUid, String channel) {
        ModelAndView modelAndView = new ModelAndView();
        List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
        String trialUid = null;
        for (Trial trial : trials)
            if (Objects.equals(trial.getStatus(), 0) || Objects.equals(trial.getStatus(), 1) || Objects.equals(trial.getStatus(), 5)) {
                trialUid = trial.getUid();
            }
        Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
        //将页面中需要的参数携带回去
        modelAndView.addObject("courtRoomUid", courtRoomUid);
        modelAndView.addObject("channel", channel);
        modelAndView.addObject("planUid", planUid);
        modelAndView.addObject("caseMap", caseMap);
        modelAndView.setViewName("judge/living");
        return modelAndView;
    }

    @RequestMapping("play")
    public ModelAndView play(String planUid,String trialUid) {
        ModelAndView modelAndView = new ModelAndView();
        if (!ObjectUtils.isEmpty(planUid)) {
            if (!ObjectUtils.isEmpty(trialUid)) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trialUid);
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trialUid);
                modelAndView.setViewName("judge/play");
                return modelAndView;
            }
            List<Trial> trials = caseVideoService.getTrialsByPlanUid(planUid);
            trials.removeIf(tempTrial -> Objects.equals(tempTrial.getStatus(), 1) || Objects.equals(tempTrial.getStatus(), 0));
            if (trials.size() == 1) {
                Map<String, Object> caseMap = casePlanService.getCaseInfoByUid(planUid, trials.get(0).getUid());
                modelAndView.addObject("caseMap", caseMap);
                modelAndView.addObject("trialUid", trials.get(0).getUid());
                modelAndView.setViewName("judge/play");
                return modelAndView;
            }
            modelAndView.addObject("trialList", trials);
            modelAndView.addObject("planUid", planUid);
        }
        modelAndView.setViewName("judge/play_select");
        return modelAndView;
    }

    @RequestMapping("screenshot")
    public ModelAndView screenshot() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
//        modelAndView.addObject("result", result);
        modelAndView.setViewName("judge/screenshot");
        return modelAndView;
    }

    @RequestMapping("favorite")
    public ModelAndView favorite() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        FavoritePojo favoritePojo = new FavoritePojo();
        favoritePojo.setUserUid(user.getUid());
        favoritePojo.setPage(1);
        favoritePojo.setPageSize(12);
        Page<FavoriteResult> favorites = favoriteService.pageFavorite(favoritePojo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<favorites.getResult().size();i++) {
            FavoriteResult favoriteResult = favorites.get(i);
            favoriteResult.setStart(format.format(favoriteResult.getStartDate()));
        }
        modelAndView.addObject("count", favorites.getTotal());
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(null);
        modelAndView.addObject("judgeList",selectJudgeAndSecretary.get("judgeList"));
        modelAndView.addObject("secretaryList",selectJudgeAndSecretary.get("secretaryList"));
        modelAndView.addObject("courts", Constants.COURT_LIST);
        modelAndView.addObject("courtrooms", Constants.COURTROOM_LIST);
        modelAndView.addObject("caseType", caseReviewService.getAllCaseType());
        modelAndView.addObject("result", favorites.getResult());
        modelAndView.setViewName("judge/favorite");
        return modelAndView;
    }
}
